<?php

namespace Question\Controller;

/**
 * 抢答
 */
class Rush extends \App\Application {

	private $_rushService;

	public function __construct($appName, $controllerName, $actionName) {
		parent::__construct($appName, $controllerName, $actionName);
		$this->_rushService = \Lsf\Loader::service('Rush', FALSE, APP_NAME_QUESTION);
	}

	/**
	 * 抢答
	 */
	public function execute() {
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		if (!isset($params['group_id']) || empty($params['group_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'group_id');
		}
		if (!isset($params['question_id']) || empty($params['question_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_id');
		}
		// 提交答案
		$result = $this->_rushService->executeRush($params['device_id'],$params['group_id'], $params['question_id']);
		if(!is_array($result)){
			if($result == -1){
				return $this->json(5,[],'未开始抢答');
			}
			return $this->json(4,[],'抢答失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 开始抢答
	 */
	public function start() {
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		$result = $this->_rushService->rushStart($params['device_id']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'开始抢答失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 抢答结果
	 */
	public function result() {
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		$result = $this->_rushService->getRushResult($params['device_id']);
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'抢答结果失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 抢答分数
	 */
	public function point() {
		$result = $this->_rushService->getRushPoint();
		if(!is_array($result) || empty($result)){
			return $this->json(4,[],'抢答分数失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 抢答答题
	 */
	public function answer() {
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		if (!isset($params['group_id']) || empty($params['group_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'group_id');
		}
		if (!isset($params['question_id']) || empty($params['question_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'question_id');
		}
		if (!isset($params['answer']) || empty($params['answer'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'answer');
		}
		// 提交答案
		$result = $this->_rushService->rushAnswer($params['device_id'],$params['group_id'], $params['question_id'],$params['answer']);
		if(!is_array($result)){
			return $this->json(4,[],'抢答失败');
		}
		if($result[0] == 3){
			return $this->json(3,[],'每道题只能回答一次');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}

	/**
	 * 抢答重置
	 */
	public function restart() {
		$params = $this->post('', TRUE);
		if (!isset($params['device_id']) || empty($params['device_id'])) {
			return $this->errParamMissing(ECODE_PARAM_MISSING, 'device_id');
		}
		// 重新开始
		$result = $this->_rushService->rushRestart($params['device_id']);
		if(!is_array($result)){
			if($result == -1){
				return $this->json(5,[],'非主持人设备');
			}
			return $this->json(4,[],'重置抢答失败');
		}
		return $this->json(ECODE_SUCCESS, $result);
	}
}
